<?php
class Zendk_Controller_Action extends Zend_Controller_Action{
   
   public function init(){
   		if($this->_request->getParam("module")=='default'){
   			$template_path = TEMPLATE_PATH . "/default/system";
   			$this->loadTemplate($template_path,'template.ini','default');
   		}else if($this->_request->getParam("module")=='admin'){
            $template_path = TEMPLATE_PATH . "/admin";
   			$this->loadTemplate($template_path,'template.ini','default');
   		}
   		
   }
   
   protected function loadTemplate($template_path, $fileConfig = 'template.ini',$sectionConfig = 'template'){
      
      //Xoa nhung du cua layout truoc
      $this->view->headTitle()->set('');
      $this->view->headMeta()->getContainer()->exchangeArray(array());
      $this->view->headLink()->getContainer()->exchangeArray(array());
      $this->view->headScript()->getContainer()->exchangeArray(array());
      
      $filename = $template_path . "/" . $fileConfig;
      $section = $sectionConfig;
      $config = new Zend_Config_Ini($filename,$section);
      $config = $config->toArray();
      
      $templateUrl = $config['url'];
      $cssUrl = $templateUrl . $config['dirCss'];
      $jsUrl    = $templateUrl . $config['dirJs'];
      $imgUrl = $templateUrl . $config['dirImg'];
      
      //Nap title cho layout
      $this->view->headTitle($config['title']);
      
      //Nap cac the meta vao layout
      if(count($config['metaHttp'])>0){      
         foreach ($config['metaHttp'] as $key => $value){
            $tmp = explode("|",$value);            
            $this->view->headMeta()->appendHttpEquiv($tmp[0],$tmp[1]);
         }
      }
      
      if(count($config['metaName'])>0){      
         foreach ($config['metaName'] as $key => $value){
            $tmp = explode("|",$value);            
            $this->view->headMeta()->appendName($tmp[0],$tmp[1]);
         }
      }
      
      //Nap cac tap tin CSS vao layout
      if(count($config['fileCss'])>0){      
         foreach ($config['fileCss'] as $key => $css){
            $this->view->headLink()->appendStylesheet($cssUrl . $css,'screen');
         }
      }
      
      //Nap cac tap tin javascript cho layout
      if(count($config['fileJs'])>0){      
         foreach ($config['fileJs'] as $key => $js){
            $this->view->headScript()->appendFile($jsUrl . $js,'text/javascript');
         }
      }
      
      if(count($config['dirs'])>0){
         foreach ($config['dirs'] as $val){
            $arrDir[$val] = $config['url']  . '/' . $val;
         }
      }
      $this->view->templateUrl = $templateUrl;
      $this->view->cssUrl = $cssUrl;
      $this->view->jsUrl = $jsUrl;
      $this->view->imgUrl = $imgUrl;
      $this->view->arrDir = $arrDir;
      
      $option = array('layoutPath'=> $template_path, 'layout'=> $config['layout']);
      Zend_Layout::startMvc($option);
      
   }
}